import { createRouter, createWebHistory } from 'vue-router'
import Login from '../components/background/Login'
import Main from '../components/Main.vue'
import Home from '../components/front/Home'
import BackgroundHome from "@/views/BackgroundHome";
import Welcome from '../pages/BackgroundWelcome'
import Content from '../components/background/Content'
import PublishGoods from '../pages/publishGoods'
import ChangePassword from "@/pages/ChangePassword";
import GoodsRecords from "@/pages/GoodsRecords";
import ShowBuyer from "@/pages/ShowBuyer";
import BuyerAlreadySelect from "@/pages/BuyerAlreadySelect";
import Goods from '../pages/Goods'
import registery from '../pages/regiest'
// import ShowUser from "@/pages/ShowUser";
// import OrdersManager from "@/pages/OrdersManager";
import orderHistory from "@/components/usercenter/OrderHistory";
import userInfo from "@/components/usercenter/UserInfo";
import UserHome from "@/components/usercenter/UserHome";
import userWelcome from "@/components/usercenter/UserWelcome";
import showUsers from "@/pages/showUsers";
import UserLogin from "@/pages/UserLogin";
import UserRegiest from "@/pages/UserRegiest";
import shoppingCar from "@/components/usercenter/ShoppingCar";
import Collection from "@/components/usercenter/Collection";
import UserOrder from "@/components/usercenter/UserOrder";
import OrderManager from "@/pages/OrderManager";
import AftersaleService from "@/pages/AftersaleService";
import Aftersale from "@/components/usercenter/Aftersale";
const routes = [
    {
        path: '/',
        name: 'Main',
        component: Main
    },
    {
        path: '/background/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/registry',
        name: 'registery',
        component: registery
    },
    {
        path: '/front/home',
        name: 'Home',
        component: Home,
    },
    {
        path: '/front/login',
        name: 'userLogin',
        component: UserLogin
    },
    {
        path: '/regiest',
        component: UserRegiest
    },
    {
        path: '/background',
        name: 'BackgroundHome',
        component: BackgroundHome,
        meta: {
            requiredToken: true
        },
        children: [
            {
                path: 'welcome',
                component: Welcome,
            },
            {
                path: 'content',
                component: Content,
            },
            {
                path: 'publishGoods',
                component: PublishGoods
            },
            {
                path: 'showUsers',
                component: showUsers
            },
            {
                path:'OrderManager',
                component:OrderManager

            },
            {
                path: 'changePassword',
                component: ChangePassword
            },
            {
                path: 'goodsRecords',
                component: GoodsRecords
            },
            {
                path: 'goods',
                component: Goods
            },
            {
                path: 'showbuyer',
                component: ShowBuyer
            },
            {
                path: 'AftersaleService',
                component: AftersaleService
            },
            {
                path: 'buyerAlreadySelect',
                component: BuyerAlreadySelect
            }
        ]
    },
    {
        path: '/userCenter',
        name: 'UserHome',
        component: UserHome,
        meta: {
            userToken: true
        },
        children: [
            {
                path: 'userWelcome',
                component: userWelcome,
            },
            {
                path: 'orderHistory',
                component: orderHistory,
            },
            {
                path: 'userInfo',
                component: userInfo,
            },
            {
                path:'shoppingCar',
                component: shoppingCar,
            },
            {
                path:'userOrder',
                component: UserOrder,
            },
            {
                path:'Aftersale',
                component: Aftersale,
            },
            {
                path:'collection',
                component: Collection,
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//全局路由设置，实现登录拦截
router.beforeEach((to, from, next) => {
    if (to.meta.requiredToken) {
        if (localStorage.getItem('token') != null) {
            next()
        } else {
            next({path: '/background/login'})
        }
    } else {
        next()
    }
})

export default router
